﻿<?php
/**
 * 调用微信JS api 支付完成后，主动查询订单状态，并修改订单状态
 */
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "lib/WxPay.Api.php";
require_once 'example/log.php';

//初始化日志
$logHandler= new CLogFileHandler("./logs/".date('Y-m-d').'.log');
$log = Log1::Init($logHandler, 15);
if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
	$out_trade_no = $_REQUEST["out_trade_no"];
	$input = new WxPayOrderQuery();
	$input->SetOut_trade_no($out_trade_no);
	$result = WxPayApi::orderQuery($input);
	if($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS'){
		if($result['trade_state'] == 'SUCCESS'){//订单支付成功
			//更新订单状态
			$con = mysql_connect("localhost","baoming","U8U6t8P3");
			if (!$con){
				die('Could not connect: ' . mysql_error());
			}
			mysql_select_db("baoming", $con);
			$pay_time = time();
			mysql_query("UPDATE bm_enroll SET state=1,pay_time=$pay_time WHERE order_sn=$out_trade_no and state=0");
			$re=mysql_query("select * from  bm_enroll where order_sn=$out_trade_no and state=1" );
			while(@$arr=mysql_fetch_assoc($re)){
				$result = $arr;
			}
			mysql_close($con);
			//跳转到支付成功页面
			header('Location:/baoming/Enroll/prompt/enroll_id/'.$result['id']);exit;
		}
	}
}
//支付失败。
header('Location:/baoming/User/user_center');exit;
?>